package com.yunchang.springboot.service.AmazonAdvert;

import com.yunchang.springboot.base.BaseService;
import com.yunchang.springboot.models.DO.*;
import com.yunchang.springboot.models.Shop;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AdvOriginalDataReportHandleService extends BaseService {

    /**
     * 处理AdvBrandsCampaignsReport
     * */
    public void handleOriginalDataAdvBrandsCampaignsReport(List<Map<String, Object>> lists, Shop shop, String countryCode, String date)
            throws Exception {
        List<Object> list = new ArrayList<>(lists.size());
        for (Map<String, Object> map : lists) {
            AmazonAdvBrandsCampaignReport report = new AmazonAdvBrandsCampaignReport();
            report.setDate(date);
            report.setSellerId(shop.getSellerId());
            report.setArea(countryCode);
            report.setCampaignName(MapUtils.getString(map, "campaignName"));
            report.setCampaignId(MapUtils.getString(map, "campaignId"));
            report.setCampaignBudget(BigDecimal.valueOf(MapUtils.getDouble(map, "campaignBudget")));
            report.setCampaignBudgetType(MapUtils.getString(map, "campaignBudgetType"));
            report.setCampaignStatus(MapUtils.getString(map, "campaignStatus"));
            report.setMatchType(MapUtils.getString(map, "matchType"));
            report.setImpressions(MapUtils.getInteger(map, "impressions"));
            report.setClicks(MapUtils.getInteger(map, "clicks"));
            report.setCost(BigDecimal.valueOf(MapUtils.getDouble(map, "cost")));
            report.setAttributedDetailPageViewsClicks14d(MapUtils.getInteger(map, "attributedDetailPageViewsClicks14d"));
            report.setAttributedSales14d(MapUtils.getInteger(map, "attributedSales14d"));
            report.setAttributedSales14dSameSku(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSales14dSameSKU")));
            report.setAttributedConversions14d(MapUtils.getInteger(map, "attributedConversions14d"));
            report.setAttributedConversions14dSameSku(MapUtils.getInteger(map, "attributedConversions14dSameSKU"));
            report.setAttributedOrdersNewToBrand14d(MapUtils.getInteger(map, "attributedOrdersNewToBrand14d"));
            report.setAttributedOrdersNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedOrdersNewToBrandPercentage14d")));
            report.setAttributedOrderRateNewToBrand14d(MapUtils.getInteger(map, "attributedOrderRateNewToBrand14d"));
            report.setAttributedSalesNewToBrand14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSalesNewToBrand14d")));
            report.setAttributedSalesNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSalesNewToBrandPercentage14d")));
            report.setAttributedUnitsOrderedNewToBrand14d(MapUtils.getInteger(map, "attributedUnitsOrderedNewToBrand14d"));
            report.setAttributedUnitsOrderedNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedUnitsOrderedNewToBrandPercentage14d")));
            report.setUnitsSold14d(MapUtils.getInteger(map, "unitsSold14d"));
            report.setDpv14d(MapUtils.getInteger(map, "dpv14d"));
            report.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            list.add(report);
        }
        baseInsertList(list);
    }

    /**
     * 处理AdvBrandsAdGroupsReport
     * */
    public void handleOriginalDataAdvBrandsAdGroupsReport(List<Map<String, Object>> lists, Shop shop, String countryCode, String date)
            throws Exception {
        List<Object> list = new ArrayList<>(lists.size());
        for (Map<String, Object> map : lists) {
            AmazonAdvBrandsAdGroupsReport report = new AmazonAdvBrandsAdGroupsReport();
            report.setDate(date);
            report.setArea(countryCode);
            report.setSellerId(shop.getSellerId());
            report.setCampaignId(MapUtils.getLong(map, "campaignId"));
            report.setCampaignName(MapUtils.getString(map, "campaignName"));
            report.setCampaignBudget(BigDecimal.valueOf(MapUtils.getDouble(map, "campaignBudget")));
            report.setCampaignBudgetType(MapUtils.getString(map, "campaignBudgetType"));
            report.setCampaignStatus(MapUtils.getString(map, "campaignStatus"));
            report.setAdGroupName(MapUtils.getString(map, "adGroupName"));
            report.setAdGroupId(MapUtils.getLong(map, "adGroupId"));
            report.setImpressions(MapUtils.getInteger(map, "impressions"));
            report.setClicks(MapUtils.getInteger(map, "clicks"));
            report.setCost(BigDecimal.valueOf(MapUtils.getDouble(map, "cost")));
            report.setAttributedDetailPageViewsClicks14d(MapUtils.getInteger(map, "attributedDetailPageViewsClicks14d"));
            report.setAttributedSales14d(MapUtils.getInteger(map, "attributedSales14d"));
            report.setAttributedSales14dSameSku(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSales14dSameSKU")));
            report.setAttributedConversions14d(MapUtils.getInteger(map, "attributedConversions14d"));
            report.setAttributedConversions14dSameSku(MapUtils.getInteger(map, "attributedConversions14dSameSKU"));
            report.setAttributedOrdersNewToBrand14d(MapUtils.getInteger(map, "attributedOrdersNewToBrand14d"));
            report.setAttributedOrdersNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedOrdersNewToBrandPercentage14d")));
            report.setAttributedOrderRateNewToBrand14d(MapUtils.getInteger(map, "attributedOrderRateNewToBrand14d"));
            report.setAttributedSalesNewToBrand14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSalesNewToBrand14d")));
            report.setAttributedSalesNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSalesNewToBrandPercentage14d")));
            report.setAttributedUnitsOrderedNewToBrand14d(MapUtils.getInteger(map, "attributedUnitsOrderedNewToBrand14d"));
            report.setAttributedUnitsOrderedNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedUnitsOrderedNewToBrandPercentage14d")));
            report.setUnitsSold14d(MapUtils.getInteger(map, "unitsSold14d"));
            report.setDpv14d(MapUtils.getInteger(map, "dpv14d"));
            report.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            list.add(report);
        }
        baseInsertList(list);
    }

    /**
     * 处理AdvBrandsKeywordsReport
     * */
    public void handleOriginalDataAdvBrandsKeywordsReport(List<Map<String, Object>> lists, Shop shop, String countryCode, String date)
            throws Exception {
        List<Object> list = new ArrayList<>(lists.size());
        for (Map<String, Object> map : lists) {
            AmazonAdvBrandsKeywordReport report = new AmazonAdvBrandsKeywordReport();
            report.setDate(date);
            report.setArea(countryCode);
            report.setSellerId(shop.getSellerId());
            report.setCampaignId(MapUtils.getLong(map, "campaignId"));
            report.setCampaignName(MapUtils.getString(map, "campaignName"));
            report.setAdGroupId(MapUtils.getLong(map, "adGroupId"));
            report.setAdGroupName(MapUtils.getString(map, "adGroupName"));
            report.setCampaignBudget(BigDecimal.valueOf(MapUtils.getDouble(map,"campaignBudget")));
            report.setCampaignBudgetType(MapUtils.getString(map, "campaignBudgetType"));
            report.setCampaignStatus(MapUtils.getString(map, "campaignStatus"));
            report.setKeywordId(MapUtils.getLong(map, "keywordId"));
            report.setKeywordStatus(MapUtils.getString(map, "keywordStatus"));
            report.setKeywordBid(BigDecimal.valueOf(MapUtils.getDouble(map, "keywordBid")));
            report.setKeywordText(MapUtils.getString(map, "keywordText"));
            report.setMatchType(MapUtils.getString(map, "matchType"));
            report.setImpressions(MapUtils.getInteger(map, "impressions"));
            report.setClicks(MapUtils.getInteger(map, "clicks"));
            report.setCost(BigDecimal.valueOf(MapUtils.getDouble(map, "cost")));
            report.setAttributedDetailPageViewsClicks14d(MapUtils.getInteger(map, "attributedDetailPageViewsClicks14d"));
            report.setAttributedSales14d(MapUtils.getInteger(map, "attributedSales14d"));
            report.setAttributedSales14dSameSku(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSales14dSameSKU")));
            report.setAttributedConversions14d(MapUtils.getInteger(map, "attributedConversions14d"));
            report.setAttributedConversions14dSameSku(MapUtils.getInteger(map, "attributedConversions14dSameSKU"));
            report.setAttributedOrdersNewToBrand14d(MapUtils.getInteger(map, "attributedOrdersNewToBrand14d"));
            report.setAttributedOrdersNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedOrdersNewToBrandPercentage14d")));
            report.setAttributedOrderRateNewToBrand14d(MapUtils.getInteger(map, "attributedOrderRateNewToBrand14d"));
            report.setAttributedSalesNewToBrand14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSalesNewToBrand14d")));
            report.setAttributedSalesNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSalesNewToBrandPercentage14d")));
            report.setAttributedUnitsOrderedNewToBrand14d(MapUtils.getInteger(map, "attributedUnitsOrderedNewToBrand14d"));
            report.setAttributedUnitsOrderedNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedUnitsOrderedNewToBrandPercentage14d")));
            report.setUnitsSold14d(MapUtils.getInteger(map, "unitsSold14d"));
            report.setDpv14d(MapUtils.getInteger(map, "dpv14d"));
            report.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            list.add(report);
        }
        baseInsertList(list);
    }

    /**
     * 处理AdvBrandsTargetsReport
     * */
    public void handleOriginalDataAdvBrandsTargetsReport(List<Map<String, Object>> lists, Shop shop, String countryCode, String date)
            throws Exception {
        List<Object> list = new ArrayList<>(lists.size());
        for (Map<String, Object> map : lists) {
            AmazonAdvBrandsTargetReport report = new AmazonAdvBrandsTargetReport();
            report.setDate(date);
            report.setArea(countryCode);
            report.setSellerId(shop.getSellerId());
            report.setCampaignId(MapUtils.getLong(map, "campaignId"));
            report.setCampaignName(MapUtils.getString(map, "campaignName"));
            report.setAdGroupId(MapUtils.getLong(map, "adGroupId"));
            report.setAdGroupName(MapUtils.getString(map, "adGroupName"));
            report.setCampaignBudgetType(MapUtils.getString(map, "campaignBudgetType"));
            report.setCampaignStatus(MapUtils.getString(map, "campaignStatus"));
            report.setTargetId(MapUtils.getString(map, "targetId"));
            report.setTargetingExpression(MapUtils.getString(map, "targetingExpression"));
            report.setTargetingType(MapUtils.getString(map, "targetingType"));
            report.setTargetingText(MapUtils.getString(map, "targetingText"));
            report.setImpressions(MapUtils.getInteger(map, "impressions"));
            report.setClicks(MapUtils.getInteger(map, "clicks"));
            report.setCost(BigDecimal.valueOf(MapUtils.getDouble(map, "cost")));
            report.setAttributedDetailPageViewsClicks14d(MapUtils.getInteger(map, "attributedDetailPageViewsClicks14d"));
            report.setAttributedSales14d(MapUtils.getInteger(map, "attributedSales14d"));
            report.setAttributedSales14dSameSku(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSales14dSameSKU")));
            report.setAttributedConversions14d(MapUtils.getInteger(map, "attributedConversions14d"));
            report.setAttributedConversions14dSameSku(MapUtils.getInteger(map, "attributedConversions14dSameSKU"));
            report.setAttributedOrdersNewToBrand14d(MapUtils.getInteger(map, "attributedOrdersNewToBrand14d"));
            report.setAttributedOrdersNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedOrdersNewToBrandPercentage14d")));
            report.setAttributedOrderRateNewToBrand14d(MapUtils.getInteger(map, "attributedOrderRateNewToBrand14d"));
            report.setAttributedSalesNewToBrand14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSalesNewToBrand14d")));
            report.setAttributedSalesNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedSalesNewToBrandPercentage14d")));
            report.setAttributedUnitsOrderedNewToBrand14d(MapUtils.getInteger(map, "attributedUnitsOrderedNewToBrand14d"));
            report.setAttributedUnitsOrderedNewToBrandPercentage14d(BigDecimal.valueOf(MapUtils.getDouble(map, "attributedUnitsOrderedNewToBrandPercentage14d")));
            report.setUnitsSold14d(MapUtils.getInteger(map, "unitsSold14d"));
            report.setDpv14d(MapUtils.getInteger(map, "dpv14d"));
            report.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            list.add(report);
        }
        baseInsertList(list);
    }

    /**
     * 处理AdvBrandsSearchTermReport
     * */
    public void handleOriginalDataAdvBrandsSearchTermReport(List<Map<String, Object>> lists, Shop shop, String countryCode, String date)
            throws Exception {
        List<Object> list = new ArrayList<>(lists.size());
        for (Map<String, Object> map : lists) {
            AmazonAdvBrandsSearchTermReport report = new AmazonAdvBrandsSearchTermReport();
            report.setDate(date);
            report.setArea(countryCode);
            report.setSellerId(shop.getSellerId());
            report.setCampaignId(MapUtils.getLong(map, "campaignId"));
            report.setCampaignName(MapUtils.getString(map, "campaignName"));
            report.setAdGroupId(MapUtils.getLong(map, "adGroupId"));
            report.setAdGroupName(MapUtils.getString(map, "adGroupName"));
            report.setCampaignBudget(BigDecimal.valueOf(MapUtils.getDouble(map, "campaignBudget")));
            report.setCampaignBudgetType(MapUtils.getString(map, "campaignBudgetType"));
            report.setCampaignStatus(MapUtils.getString(map, "campaignStatus"));
            report.setKeywordId(MapUtils.getLong(map, "keywordId"));
            report.setKeywordStatus(MapUtils.getString(map, "keywordStatus"));
            report.setKeywordBid(BigDecimal.valueOf(MapUtils.getDouble(map, "keywordBid")));
            report.setKeywordText(MapUtils.getString(map, "keywordText"));
            report.setMatchType(MapUtils.getString(map, "matchType"));
            report.setImpressions(MapUtils.getInteger(map, "impressions"));
            report.setClicks(MapUtils.getInteger(map, "clicks"));
            report.setCost(BigDecimal.valueOf(MapUtils.getDouble(map, "cost")));
            report.setAttributedConversions14d(MapUtils.getInteger(map, "attributedConversions14d"));
            report.setAttributedSales14d(MapUtils.getInteger(map, "attributedSales14d"));
            report.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            list.add(report);
        }
        baseInsertList(list);
    }
}
